package com.gla.giliair.web.admin;

import com.gla.giliair.entity.Company;
import com.gla.giliair.entity.CompanyPlanemodel;
import com.gla.giliair.util.MapGenerator;
import com.gla.giliair.web.BaseController;
import com.gla.giliair.web.vo.Cus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Description:
 * @Author: Yingtian qian
 * @Date: 2021/8/25 20:39
 */
@Api(tags = "航空公司管理模块")
@RestController
@RequestMapping("/api/adminX")
public class AdminCompanyController extends BaseController {
    @ApiOperation(value = "获取全部航空公司列表", notes = "获取全部航空公司列表，有多种排序方式", produces = "application/json")
    @GetMapping("/findAllCompany")
    @ResponseBody
    public Map<String, Object> findAllCompany(//排序关键词：companyId、companyName、companyCode、
                                              // phone、url、introduction、
                                              // createTime、updateTime、isDeleted
                                              //不填关键词默认按id逆向排序
                                              @RequestParam(value = "sort", defaultValue = "companyId") String sort,
                                              //正反序只对关键词生效
                                              @RequestParam(value = "isASC", defaultValue = "false") boolean isASC,
                                              @RequestParam(value = "showDeleted", defaultValue = "false") boolean showDeleted,
                                              @RequestParam(value = "page", defaultValue = "1") Integer page,
                                              @RequestParam(value = "pageSize", defaultValue = "16") Integer pageSize) {
        return companyService.findAllCompanies(sort, isASC, showDeleted, page, pageSize);
    }

    @ApiOperation(value = "搜索航空公司列表", notes = "搜索航空公司列表，有多种排序方式", produces = "application/json")
    @PostMapping("/searchCompany")
    @ResponseBody
    public Map<String, Object> searchCompany(//排序关键词：companyId、companyName、companyCode、
                                             // phone、url、introduction、
                                             // createTime、updateTime、isDeleted
                                             //不填关键词默认按id逆向排序
                                             @RequestParam(value = "sort", defaultValue = "companyId") String sort,
                                             //正反序只对关键词生效
                                             @RequestParam(value = "isASC", defaultValue = "false") boolean isASC,
                                             @RequestParam(value = "showDeleted", defaultValue = "false") boolean showDeleted,
                                             @RequestParam(value = "page", defaultValue = "1") Integer page,
                                             @RequestParam(value = "pageSize", defaultValue = "16") Integer pageSize,
                                             //模糊搜索参数：名称、代号、电话
                                             @RequestBody Cus searchParams) {
        return companyService.searchCompanies(sort, isASC, showDeleted, page, pageSize, searchParams);
    }

    @ApiOperation(value = "添加航空公司信息", notes = "添加航空公司信息", produces = "application/json")
    @PostMapping("/addCompany")
    @ResponseBody
    public Map<String, Object> addCompany(@RequestParam("companyCode") String companyCode,
                                          @RequestParam("companyName") String companyName,
                                          @RequestParam(value = "phone", required = false) String phone,
                                          @RequestParam(value = "url", required = false) String url,
                                          @RequestParam(value = "introduction", required = false) String introduction) {
        if (!StringUtils.hasText(companyCode)) {
            return MapGenerator.getRes(500, "请输入航空公司代号！");
        }
        if (!StringUtils.hasText(companyName)) {
            return MapGenerator.getRes(501, "请输入航空公司名！");
        }

        Company company = new Company();
        company.setCompanyCode(companyCode);
        company.setCompanyName(companyName);
        if (!phone.equals("")) {
            company.setPhone(phone);
        }
        if (!url.equals("")) {
            company.setUrl(url);
        }
        if (!introduction.equals("")) {
            company.setIntroduction(introduction);
        }
        return companyService.addCompany(company);
    }

    @ApiOperation(value = "修改航空公司信息", notes = "修改航空公司信息，禁止修改航空公司代号", produces = "application/json")
    @PostMapping("/modifyCompany")
    @ResponseBody
    public Map<String, Object> modifyCompany(@RequestParam("companyId") Long companyId,
                                             @RequestParam(value = "companyName", required = false) String companyName,
                                             @RequestParam(value = "phone", required = false) String phone,
                                             @RequestParam(value = "url", required = false) String url,
                                             @RequestParam(value = "introduction", required = false) String introduction) {
        if (companyId == null) {
            return MapGenerator.getRes(500, "请输入航空公司id！");
        }

        Company company = new Company();
        company.setCompanyId(companyId);
        if (!companyName.equals("")) {
            company.setCompanyName(companyName);
        }
        if (!phone.equals("")) {
            company.setPhone(phone);
        }
        if (!url.equals("")) {
            company.setUrl(url);
        }
        if (!introduction.equals("")) {
            company.setIntroduction(introduction);
        }

        return companyService.modifyCompany(company);
    }

    @ApiOperation(value = "删除航空公司信息", notes = "删除航空公司信息", produces = "application/json")
    @PostMapping("/deleteCompany")
    @ResponseBody
    public Map<String, Object> deleteCompany(@RequestParam("companyId") Long companyId) {
        if (companyId == null) {
            return MapGenerator.getRes(500, "请输入航空公司id！");
        }
        return companyService.deleteCompany(companyId);
    }

    @ApiOperation(value = "获取航空公司所属机型列表", notes = "获取航空公司所属机型列表", produces = "application/json")
    @GetMapping("/findCompanyPlanemodel")
    @ResponseBody
    public Map<String, Object> findCompanyPlanemodel(//默认按planemodelId正向排序
                                                     @RequestParam("companyId") Long companyId,
                                                     @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pageSize", defaultValue = "16") Integer pageSize) {
        return companyPlanemodelService.findPlanemodelsByCompanyId(companyId, page, pageSize);
    }

    @ApiOperation(value = "添加航空公司所属机型", notes = "添加航空公司所属机型", produces = "application/json")
    @PostMapping("/addCompanyPlanemodel")
    @ResponseBody
    public Map<String, Object> addCompanyPlanemodel(@RequestParam("companyId") Long companyId,
                                                    @RequestParam("planemodelId") Long planemodelId) {
        if (companyId == null) {
            return MapGenerator.getRes(500, "请输入航空公司id！");
        }
        if (planemodelId == null) {
            return MapGenerator.getRes(501, "请输入飞机机型id！");
        }

        CompanyPlanemodel companyPlanemodel = new CompanyPlanemodel();
        companyPlanemodel.setCompanyId(companyId);
        companyPlanemodel.setPlanemodelId(planemodelId);

        return companyPlanemodelService.addCompanyPlanemodel(companyPlanemodel);
    }

    @ApiOperation(value = "删除航空公司所属机型", notes = "删除航空公司所属机型", produces = "application/json")
    @PostMapping("/deleteCompanyPlanemodel")
    @ResponseBody
    public Map<String, Object> deleteCompanyPlanemodel(@RequestParam("companyPlanemodelId") Long companyPlanemodelId) {
        if (companyPlanemodelId == null) {
            return MapGenerator.getRes(500, "请输入航空公司所属机型id！");
        }
        return companyPlanemodelService.deleteCompanyPlanemodel(companyPlanemodelId);
    }
}


